package com.su.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.commonutils.R;
import com.su.conutils.ConUtil;
import com.su.service.LossfoundService;
import com.su.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LossfoundServiceImpl implements LossfoundService {
    @Override
    public Object getlossinfo(Map<String,Object> map) {
        String method = "get";
        String host = "https://shixin.market.alicloudapi.com";
        String path = "/creditblacklist/query";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + ConUtil.getkey());
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, Object> querys = new HashMap<String, Object>();
        querys.put("idcard", map.get("idcard"));
        querys.put("realname", map.get("name"));
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            //System.out.println(response.toString());
            //获取response的body
//            System.out.println(EntityUtils.toString(response.getEntity(),"utf-8"));
            JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(response.getEntity(), "utf-8"));

            return jsonObject.get("result");

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
